package qc.module.ehs.mapper;

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
import qc.common.core.enums.EhsRiskEvaluationMethodEnum;
import qc.common.core.enums.EhsRiskLevelEnum;
import qc.module.ehs.dto.riskevaluation.HazardRiskEvaluationDto;
import qc.module.ehs.entity.EhsHazardRiskEvaluation;

import java.util.List;

/**
 * EhsHazardRiskevlMapper
 *
 * @author QuCheng Tech
 * @create 2024/11/5
 */
@Mapper
public interface EhsHazardRiskEvaluationMapper {

    EhsHazardRiskEvaluationMapper MAPPER = Mappers.getMapper(EhsHazardRiskEvaluationMapper.class);

    @Mapping(target = "tm", source = "tm", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "intm", source = "intm", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "method", source = "method.index")
    @Mapping(target = "risklevel", source = "risklevel.index")
    HazardRiskEvaluationDto toDto(EhsHazardRiskEvaluation en);
    
    List<HazardRiskEvaluationDto> toDtoList(List<EhsHazardRiskEvaluation> ens);

    @Mapping(target = "tm", source = "tm", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "intm", source = "intm", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "method", ignore = true)
    @Mapping(target = "risklevel", ignore = true)
    EhsHazardRiskEvaluation toEntity(HazardRiskEvaluationDto dto);

    default EhsHazardRiskEvaluation dtoToEntity(HazardRiskEvaluationDto dto) {
        EhsHazardRiskEvaluation en = EhsHazardRiskEvaluationMapper.MAPPER.toEntity(dto);
     
        en.setMethod(getEhsRiskEvaluationMethodEnum(dto.getMethod()));
        en.setRisklevel(getEhsRiskLevelEnum(dto.getRisklevel()));

        return en;
    }

    static EhsRiskEvaluationMethodEnum getEhsRiskEvaluationMethodEnum(Integer index) {
        if (index != null && index > 0x0) {
            for (EhsRiskEvaluationMethodEnum item : EhsRiskEvaluationMethodEnum.values()) {
                if (item.getIndex() == index) {//判断序号使用getIndex，判断内容使用getName
                    return item;
                }
            }
        }

        //默认为null，风险评价方法必须有
        return null;
    }

    static EhsRiskLevelEnum getEhsRiskLevelEnum(Integer index) {
        if (index != null && index > 0x0) {
            for (EhsRiskLevelEnum item : EhsRiskLevelEnum.values()) {
                if (item.getIndex() == index) {//判断序号使用getIndex，判断内容使用getName
                    return item;
                }
            }
        }

        //默认为null，风险等级必须有
        return null;
    }
}
